class WebsController < ApplicationController
  layout(false)
  # GET /admin_webs
  # GET /admin_webs.xml
  def index
    @search = Web.new_search(params[:search])
    unless params[:keywords].blank?
      keywords = split_keyword(params[:keywords])
      keywords.each do |keyword|
        unless keyword.blank?
          @search.conditions.group do |g|
            g.or_body_like = keyword
            g.or_sort_name_like = keyword
            g.or_body_contains  = keyword
          end
        end
      end
    end
    if @search.order_as.blank?
      @search.order_as = "desc"
      @search.order_by = ['sort','updated_at']
    end
    @webs = @search.all.paginate(:page => params[:page] , :per_page => 50)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @webs }
    end
  end

  # GET /admin_webs/1
  # GET /admin_webs/1.xml
  def show
    @web = Web.find_by_name(params[:id])
    if @web.blank?
      @web = Web.find_by_id(params[:id])
    end
    if @web.blank?
      @web = Web.find(:first,:order => "sort desc")
    end

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @web }
    end
  end

  # GET /admin_webs/new
  # GET /admin_webs/new.xml
  def new
    @web = Web.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @web }
    end
  end

  # GET /admin_webs/1/edit
  def edit
    @web = Web.find(params[:id]) if current_user && current_user.is_admim?
  end

  def create
    @web = Web.new(params[:web])
    @web.body << "<br/>IP:#{request.remote_addr} ,来自#{request.referer} ，主机名：#{request.remote_host}" if @web.body
    @webs = Web.find(:all , :conditions => ["'#{Time.now.strftime('%Y-%m-%d 00:00:00')} '< created_at < '#{Time.now.to_s(:db)}'"])
    respond_to do |format|
      if @webs.size > 100
        redirect_to webs_path
        flash[:notice] = '每天最多发布100个，明天再发布吧！或者联系管理员！QQ：601259096'
      else
        if @web.save
          flash[:notice] = '创建成功'
          format.html { redirect_to webs_path }
          format.xml  { render :xml => @web, :status => :created, :location => @web }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @web.errors, :status => :unprocessable_entity }
        end
      end

    end
  end

  def update
    @web = Web.find(params[:id]) if current_user && current_user.is_admim?
    respond_to do |format|
      if @web.update_attributes(params[:web])
        flash[:notice] = 'Web was successfully updated.'
        format.html { redirect_to  webs_path }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @web.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    
    @web = Web.find(params[:id]) if current_user && current_user.is_admim?
    @web.destroy

    respond_to do |format|
      format.html { redirect_to(admin_webs_url) }
      format.xml  { head :ok }
    end
  end


end
